package com.sgb.kjwl.view.ui.activity.manage;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.bumptech.glide.Glide;
import com.luck.picture.lib.entity.LocalMedia;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityWebsiteZxzzReleaseBinding;
import com.sgb.kjwl.di.manage.DaggerEnterpriseManageComponent;
import com.sgb.kjwl.di.manage.EnterpriseManageModule;
import com.sgb.link.entity.PickFilterEntity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.manage.UpdataSpecialQualificationEntity;
import com.sgb.kjwl.view.ui.adapter.manage.AddViewAdapter;
import com.sgb.kjwl.view.widget.SelectAlbumPopWindow;
import com.sgb.link.view.widget.TwoLevelMultipleChoiceWindow;
import com.sgb.kjwl.viewmodel.manage.WebsiteUpDataZhuanXiangZiZhiModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

/**
 * @author 张驰
 * @date 2021/5/13 17:15
 * @Description 网站发布，知识产权发布功能界面
 * @name com.sgb.kjwl.view.ui.activity.manage
 */
public class WebsiteUpDataZhuanXiangZiZhiActivity extends WebSiteBuildBaseActivity implements ItemInputTextView.OnWholeItemClickListener {

    @Inject
    WebsiteUpDataZhuanXiangZiZhiModel viewModel;
    private ActivityWebsiteZxzzReleaseBinding binding;

    private String companyNo = ""; //编辑时，编辑编号
    private int siteType = 1; //网站类型  0-个人 1-企业 2-团队 默认为1
    private String title = ""; // 当前编辑的类型
    private boolean isInItView = true; // 是不是初始化
    private boolean isGrade = false; // 能不能操作资质等级
    //相册选择弹窗
    SelectAlbumPopWindow selectAlbumPopWindow;
    List<LocalMedia> selectionMedia = new ArrayList<>();
    PickSelectEntity<List<PickSelectEntity>> listPickSelectEntity;
    private int onePos, twoPos; //备案单位回显，定位
    private List<PickSelectEntity<List<PickSelectEntity>>> oneData = new ArrayList<>(); //备案单位一级数据
    private List<PickFilterEntity> twoData = new ArrayList<>(); //备案单位二级数据
    private AddViewAdapter mAddViewAdapter;

    /**
     *
     * @param context
     * @param id 网站编号
     */
    public static void start(Context context,String modelTitle, String id) {
        //网站类型  0-个人 1-企业 2-团队 默认为1
        int siteType = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 1);
        start(context,modelTitle,id,siteType);
    }

    /**
     * 企业网店 知识产权发布功能界面
     *
     * @param context //     * @param modelTitle 名称
     * @param companyNo 编号
     * @param siteType 网站类型  0-个人 1-企业 2-团队 默认为1
     */
    public static void start(Context context, String modelTitle, String companyNo,int siteType) {
        Intent intent = new Intent(context, WebsiteUpDataZhuanXiangZiZhiActivity.class);
        intent.putExtra("ModelTitle", modelTitle);
        intent.putExtra("companyNo", companyNo);
//        intent.putExtra("siteNo",siteNo);
        intent.putExtra("siteType",siteType);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerEnterpriseManageComponent.builder().appComponent(appComponent).
                enterpriseManageModule(new EnterpriseManageModule(this)).
                build().inject(this);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(WebsiteUpDataZhuanXiangZiZhiActivity.this, R.layout.activity_website_zxzz_release);
        binding.setViewModel(viewModel);
        title = getIntent().getStringExtra("ModelTitle");
        binding.include.titleView.setText("专项资质");
        binding.include.ivBack.setOnClickListener(this);
        binding.tvCompanyType.setOnWholeItemClickListener(this);
        binding.tvFidleGroup.setOnWholeItemClickListener(this);
        binding.llFidleType.setOnClickListener(this);
        binding.tvPermission.setOnWholeItemClickListener(this);
        binding.tvTime.setOnWholeItemClickListener(this);
        binding.tvCertificateTime.setOnWholeItemClickListener(this);
        binding.tvUpdate.setOnClickListener(this);
        binding.ivUpdataImage.setOnClickListener(this);
        binding.tvHangyeType.setOnWholeItemClickListener(this);
        binding.ivDelect.setOnClickListener(this);
        companyNo = getIntent().getStringExtra("companyNo");
        siteType = getIntent().getIntExtra("siteType", 1);
        binding.etName.setLabelHint(String.format(getResources().getString(R.string.swk_input_hint), title));
        binding.tvCompanyType.setLabelHint(String.format(getResources().getString(R.string.swk_select_type_hint), title));
        mAddViewAdapter = new AddViewAdapter(this);
        binding.addView.setAdapter(mAddViewAdapter);
        binding.tvCompanyNumber.setInfilterType(ItemInputTextView.TYPE_INDO);
        initDataBack();
    }

    private void initDataBack() {
        viewModel.getMessage().observe(this,new Observer<String>() {
            @Override
            public void onChanged(String s) {
                if (!TextUtils.isEmpty(s)){
                    MToast.showToast(getActivity(), s);
                    finish();
                    return;
                }
            }
        });
        viewModel.getMutableLiveData().observe(this, new Observer<UpdataSpecialQualificationEntity>() {
            @Override
            public void onChanged(UpdataSpecialQualificationEntity specialQualificationEntity) {
                setUpdateData(specialQualificationEntity);
                if (specialQualificationEntity.getCustomFieldList() != null && !specialQualificationEntity.getCustomFieldList().isEmpty()){
                    binding.addView.setData(specialQualificationEntity.getCustomFieldList());
                }
            }
        });
        viewModel.getTypePickSelectList().observe(this, new Observer<List<PickSelectEntity<List<PickSelectEntity>>>>() {
            @Override
            public void onChanged(List<PickSelectEntity<List<PickSelectEntity>>> pickSelectEntityList) {
                if (isInItView) return;
                if (pickSelectEntityList.size() == 0) {
//                    MToast.showToast(getActivity(), "暂无数据");
                    return;
                }
                // 当专项资质编辑功能的时候，假如没有重新选择资质类型，直接点击资质等级的时候，需要重新获取类型列表，然后回显等级列表
                if (isGrade) { // 回显等级列表
                    for (PickSelectEntity<List<PickSelectEntity>> pickSelectEntity : pickSelectEntityList) {
                        String type = viewModel.getMutableLiveData().getValue().getType();
                        if (type == null) return;
                        if (type.equals(pickSelectEntity.getPickerViewText())) {
                            List<PickSelectEntity> list = pickSelectEntity.getChildren();
                            viewModel.getNumberPickSelectList().postValue(list);
                        }
                    }
                    return;
                }
                showOnlyOneTypeWin(pickSelectEntityList, "类型", "请选择资质类型");
            }
        });
        viewModel.getTypeHangyeSelectList().observe(this, new Observer<List<PickSelectEntity<List<PickSelectEntity>>>>() {
            @Override
            public void onChanged(List<PickSelectEntity<List<PickSelectEntity>>> pickSelectEntities) {
                if (pickSelectEntities.size() == 0) {
//                    MToast.showToast(getActivity(), "暂无数据");
                    return;
                }
                showOnlyOneTypeWin(pickSelectEntities, "行业类型", "请选择行业类型");
            }
        });
        viewModel.getPermissionPickSelectList().observe(this, new Observer<List<PickSelectEntity>>() {
            @Override
            public void onChanged(List<PickSelectEntity> pickSelectEntityList) {
                if (isInItView) return;
                if (pickSelectEntityList.size() == 0) {
//                    MToast.showToast(getActivity(), "暂无数据");
                    return;
                }
                showOnlyTypeWin(pickSelectEntityList, "权限", "请选择展示权限");
            }
        });
        viewModel.getUrlString().observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                showAvatar(s);
            }
        });
        //备案单位
        viewModel.getFieldPickSelectList().observe(this, new Observer<List<PickSelectEntity<List<PickSelectEntity>>>>() {
            @Override
            public void onChanged(List<PickSelectEntity<List<PickSelectEntity>>> pickSelectEntityList) {
                if (isInItView) return;
                if (pickSelectEntityList.size() == 0) {
//                    MToast.showToast(getActivity(), "暂无数据");
                    return;
                }
                //如果是数据回显，定位回显出来的选择样式
                //upDataToolSize(pickSelectEntityList);
                //showOnlyOneTypeWin(pickSelectEntityList, "单位", "请选择备案单位");
                showRecordUnit(pickSelectEntityList);//备案单位
            }
        });

        viewModel.getNumberPickSelectList().observe(this, new Observer<List<PickSelectEntity>>() {
            @Override
            public void onChanged(List<PickSelectEntity> pickSelectEntityList) {
                if (isInItView) return;
                if (pickSelectEntityList.size() == 0) {
                    binding.tvFidleGroup.setVisibility(View.GONE);
//                    MToast.showToast(getActivity(), "暂无数据");
                    return;
                }
                showOnlyTypeWin(pickSelectEntityList, "等级", "请选择资质等级");
            }
        });
    }

    private void setUpdateData(UpdataSpecialQualificationEntity specialQualificationEntity) {
        binding.setData(specialQualificationEntity);
        if (isInItView){
            if (!TextUtils.isEmpty(specialQualificationEntity.getQualificationClassName())){
                binding.tvFidleGroup.setVisibility(View.VISIBLE);
            }else{
                binding.tvFidleGroup.setVisibility(View.GONE);
            }
        }
        //专项资质备案单位回显
        renderUnit(specialQualificationEntity);
    }

    private void renderUnit(UpdataSpecialQualificationEntity entity) {
        String recordEnterNo = entity.getRecordEnterNo();
        String recordEnterName = entity.getRecordEnterName();
        if (TextUtils.isEmpty(recordEnterName) || TextUtils.isEmpty(entity.getRecordEnterNo())){
            return;
        }
        String[] splitNo = recordEnterNo.split(",");
        String[] splitName = recordEnterName.split(",");
        if (splitNo.length != splitName.length){
            return;
        }
        PickFilterEntity filterEntity;
        twoData.clear();
        for (int i = 0; i < splitNo.length; i++) {
            filterEntity = new PickFilterEntity();
            filterEntity.setNo(splitNo[i]);
            filterEntity.setName(splitName[i]);
            twoData.add(filterEntity);
        }
    }

    /*
     * 请求备案单位返回的数据
     * */
    public void showRecordUnit(List<PickSelectEntity<List<PickSelectEntity>>> list) {
        TwoLevelMultipleChoiceWindow mWindow = new TwoLevelMultipleChoiceWindow(this);
        mWindow.setTitle("请选择备案单位");
        mWindow.setBgView(binding.grayLayout);
        List<PickFilterEntity<List<PickFilterEntity>>> realList = new ArrayList<>();
        List<PickFilterEntity> realSecondList;
        PickFilterEntity realEntity = null;
        PickFilterEntity realSecondEntity = null;
        for (PickSelectEntity<List<PickSelectEntity>> item : list){
            realSecondList = new ArrayList<>();
            realEntity = new PickFilterEntity();
            if (item.getChildren() != null && !item.getChildren().isEmpty()){
                for (PickSelectEntity secondItem : item.getChildren()) {
                    realSecondEntity = new PickFilterEntity();
                    realSecondEntity.setNo(secondItem.getNo());
                    realSecondEntity.setName(secondItem.getProjectscale());
                    realSecondList.add(realSecondEntity);
                }
            }
            realEntity.setNo(item.getNo());
            realEntity.setName(item.getProjectscale());
            realEntity.setChildren(realSecondList);
            realList.add(realEntity);
        }
        mWindow.setOnResultClick(list1 -> {
            StringBuilder builder = new StringBuilder();
            StringBuilder builder1 = new StringBuilder();
            for (PickFilterEntity entity : list1) {
                builder.append(entity.getName()).append(",");
                builder1.append(entity.getNo()).append(",");
            }
            setProvice(realList, list1);
            viewModel.getMutableLiveData().getValue().setRecordEnterName(builder.substring(0, builder.length() - 1));
            viewModel.getMutableLiveData().getValue().setRecordEnterNo(builder1.substring(0, builder1.length() - 1));
            binding.tvFidleType.setText(viewModel.getMutableLiveData().getValue().getProvinceName() + builder.substring(0, builder.length() - 1));
            twoData.clear();
            twoData.addAll(list1);
        });
        mWindow.setData(realList, twoData);
        mWindow.showPopwindow(binding.tvFidleType, Gravity.BOTTOM, 0, 0);
    }


    //获取省份
    private void setProvice(List<PickFilterEntity<List<PickFilterEntity>>> list, List<PickFilterEntity> list1) {
        PickFilterEntity entity = list1.get(0);
        for (PickFilterEntity<List<PickFilterEntity>> item : list){
            if (item.getChildren() != null && !item.getChildren().isEmpty()){
                if (item.getChildren().contains(entity)){
                    viewModel.getMutableLiveData().getValue().setProvinceId(item.getNo());
                    viewModel.getMutableLiveData().getValue().setProvinceName(item.getName());
                    break;
                }
            }
        }
    }





//    /**
//     * 如果是数据回显，定位回显出来的选择样式
//     *
//     * @param pickSelectEntityList 要筛选的数据
//     */
//    private void upDataToolSize(List<PickSelectEntity<List<PickSelectEntity>>> pickSelectEntityList) {
//        if (viewModel == null || viewModel.getMutableLiveData() == null || viewModel.getMutableLiveData().getValue() == null){
//            return;
//        }
//        oneData.addAll(pickSelectEntityList);
//        for (int i = 0; i < pickSelectEntityList.size(); i++) {
//            PickSelectEntity<List<PickSelectEntity>> listPickSelectEntity = pickSelectEntityList.get(i);
//            if (listPickSelectEntity.getChildren() == null) {
//                listPickSelectEntity.setChildren(new ArrayList<>());
//            }
//            twoData.add(listPickSelectEntity.getChildren());
//            if (listPickSelectEntity.getNo().equals(viewModel.getMutableLiveData().getValue().getProvinceId())) {
//                onePos = i;
//                List<PickSelectEntity> pickSelectEntities = listPickSelectEntity.getChildren();
//                for (int j = 0; j < pickSelectEntities.size(); j++) {
//                    PickSelectEntity pickSelectEntity = pickSelectEntities.get(j);
//                    if (pickSelectEntity.getNo().equals(viewModel.getMutableLiveData().getValue().getRecordEnterNo())) {
//                        twoPos = j;
//                        break;
//                    }
//                }
//            }
//        }
//    }

    @Override
    protected void init() {
        if (!TextUtils.isEmpty(companyNo)) {
            viewModel.getSpecialQualificationDetail(WebsiteUpDataZhuanXiangZiZhiActivity.this, companyNo);
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_update:
                if (!ClickUtils.isDoubleClick()) {
                    Map<String, Object> map = saveOrUpData();
                    if (map == null) return;
                    viewModel.saveOrUpdate(getActivity(), map);
                }
                break;
            case R.id.iv_updata_image: //图片上传
                isInItView = false;
                if (selectAlbumPopWindow == null) {
                    initAlbumPop();
                }
                showAlbumPop();
                break;
            case R.id.iv_delect: //清空图片
                isInItView = false;
                showAvatar("");
                break;
            case R.id.ll_fidle_type:  //备案单位
                isInItView = false;
                if (viewModel.getFieldPickSelectList().getValue().size() == 0) {
                    viewModel.findProvinceRecordUnit(getActivity());
                } else {
                    showRecordUnit(viewModel.getFieldPickSelectList().getValue());
                }
                break;
        }
    }

    @Override
    public void OnWholeItemClick(View view) {
        switch (view.getId()) {
            case R.id.tv_companyType: //资质类型
                isInItView = false;
                if (viewModel.getTypePickSelectList().getValue().size() == 0) {
                    viewModel.getQualifyTypeTreeList(getActivity());
                } else {
                    showOnlyOneTypeWin(viewModel.getTypePickSelectList().getValue(), "类型", "请选择资质类型");
                }
                break;
            case R.id.tv_hangyeType://行业类型
                if (viewModel.getTypeHangyeSelectList().getValue().size() == 0) {
                    viewModel.industryList(getActivity());
                } else {
                    showOnlyOneTypeWin(viewModel.getTypeHangyeSelectList().getValue(), "行业类型", "请选择行业类型");
                }
                break;
            case R.id.tv_fidle_group:  //资质等级
                isInItView = false;
                isGrade = false;
                if (viewModel.getNumberPickSelectList().getValue().size() == 0) {
                    //当专项资质编辑功能的时候，假如没有重新选择资质类型，直接点击资质等级的时候，需要重新获取类型列表，然后回显等级列表
                    if (listPickSelectEntity == null) {
                        isGrade = true;
                        viewModel.getQualifyTypeTreeList(getActivity());
                        return;
                    }
                    viewModel.getNumberPickSelectList().postValue(listPickSelectEntity.getChildren());
                } else {
                    showOnlyTypeWin(viewModel.getNumberPickSelectList().getValue(), "等级", "请选择资质等级");
                }
                break;
            case R.id.tv_time: //登记时间
                isInItView = false;
                showCourseTimePop("请选择登记时间", true);
                break;
            case R.id.tv_permission: //权限选择
                isInItView = false;
                if (viewModel.getPermissionPickSelectList().getValue().size() == 0) {
                    viewModel.getSBOption(getActivity());
                } else {
                    showOnlyTypeWin(viewModel.getPermissionPickSelectList().getValue(), "权限", "请选择展示权限");
                }
                break;
            case R.id.tv_certificateTime://发证时间
                isInItView = false;
                showCourseTimePop("请选择发证时间", false);
                break;

        }
    }

    private Map<String, Object> saveOrUpData() {
        //mTwoLevelMultipleChoiceWindow.getProList();
        Map<String, Object> map = new HashMap<>();
        if (TextUtils.isEmpty(viewModel.getMutableLiveData().getValue().getQualificationTypeNo())) {
            MToast.showToast(getActivity(), "请选择资质类型");
            return null;
        }
        map.put("qualificationTypeNo", viewModel.getMutableLiveData().getValue().getQualificationTypeNo());//资质类型no
        map.put("type", viewModel.getMutableLiveData().getValue().getType());//资质类型
        map.put("qualificationClassNo", viewModel.getMutableLiveData().getValue().getQualificationClassNo());
        map.put("qualificationClassName", viewModel.getMutableLiveData().getValue().getQualificationClassName());

        if (listPickSelectEntity != null && listPickSelectEntity.getChildren() != null && listPickSelectEntity.getChildren().size() != 0){
            if (TextUtils.isEmpty(viewModel.getMutableLiveData().getValue().getQualificationClassNo())) {
                MToast.showToast(getActivity(), "请选择资质等级");
                return null;
            }
            map.put("qualificationClassNo", viewModel.getMutableLiveData().getValue().getQualificationClassNo());//资质等级no
        }

        if (viewModel.getMutableLiveData().getValue().getCertificateTime() == 0) {
            MToast.showToast(getActivity(), "请选择发证时间");
            return null;
        }
        map.put("certificateTime", viewModel.getMutableLiveData().getValue().getCertificateTime());

        if (TextUtils.isEmpty(viewModel.getMutableLiveData().getValue().getRecordEnterName())) {
            MToast.showToast(getActivity(), "请选择备案单位");
            return null;
        }
        map.put("recordUnit", viewModel.getMutableLiveData().getValue().getRecordEnterName());//备案单位
        map.put("recordUnitNo", viewModel.getMutableLiveData().getValue().getRecordEnterNo());//备案单位No

        map.put("provinceId", viewModel.getMutableLiveData().getValue().getProvinceId());//备案单位所在省份id
        map.put("provinceName", viewModel.getMutableLiveData().getValue().getProvinceName());//备案单位所在省份名称

        if (viewModel.getMutableLiveData().getValue().getRegisterTime() == 0) {
            MToast.showToast(getActivity(), "请选择登记时间");
            return null;
        }
        map.put("registerTime", viewModel.getMutableLiveData().getValue().getRegisterTime());//申请日期


        if (TextUtils.isEmpty(viewModel.getMutableLiveData().getValue().getShowPermission())) {
            MToast.showToast(getActivity(), "请选择展示权限");
            return null;
        }
        map.put("showPermission", viewModel.getMutableLiveData().getValue().getShowPermission());//展示权限


        if (TextUtils.isEmpty(viewModel.getMutableLiveData().getValue().getName())) {
            MToast.showToast(getActivity(), "请输入" + title + "名称");
            return null;
        }
        map.put("name", viewModel.getMutableLiveData().getValue().getName());//资质名称

        if (TextUtils.isEmpty(viewModel.getMutableLiveData().getValue().getQualificationNumber())) {
            MToast.showToast(getActivity(), "请输入资质编号");
            return null;
        }
        map.put("qualificationNumber", viewModel.getMutableLiveData().getValue().getQualificationNumber());

        if (TextUtils.isEmpty(viewModel.getMutableLiveData().getValue().getIssuingAuthority())) {
            MToast.showToast(getActivity(), "请输入发证单位");
            return null;
        }
        map.put("issuingAuthority", viewModel.getMutableLiveData().getValue().getIssuingAuthority());

        if (TextUtils.isEmpty(viewModel.getMutableLiveData().getValue().getImage())) {
            MToast.showToast(getActivity(), "请上传照片");
            return null;
        }
        if (TextUtils.isEmpty(viewModel.getMutableLiveData().getValue().getIndustryTypeNo())){
            MToast.showToast(getActivity(), "请选择行业类型");
            return null;
        }
        map.put("industryTypeNo",viewModel.getMutableLiveData().getValue().getIndustryTypeNo());
        map.put("industryTypeName",viewModel.getMutableLiveData().getValue().getIndustryTypeName());
        map.put("image", viewModel.getMutableLiveData().getValue().getImage());//图片上传
        map.put("siteType", siteType + "");//网站类型  0-个人 1-企业 2-团队
//        map.put("companyNo",);
        if (!TextUtils.isEmpty(viewModel.getMutableLiveData().getValue().getId())) {
            map.put("id", viewModel.getMutableLiveData().getValue().getId());
        }
        map.put("customFieldList", binding.addView.getDate());
        return map;
    }
    public void setview(UpdataSpecialQualificationEntity updataSpecialQualificationEntity){
        binding.tvHangyeType.getContent_edt().setText(updataSpecialQualificationEntity.getIndustryTypeName());

    }
    //时间选择
    private void showCourseTimePop(String title, boolean isRegister) {
//        Calendar dayCalendar = Calendar.getInstance();
//        dayCalendar.setTime(new Date());
//        dayCalendar.add(Calendar.DAY_OF_MONTH, 1);

        TimePickerView timePicker = new TimePickerBuilder(this, (date, v) -> {
            @SuppressLint("SimpleDateFormat")
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(date);
            try {
                if (isRegister){
                    viewModel.getMutableLiveData().getValue().setRegisterTime(date.getTime());
                } else {
                    viewModel.getMutableLiveData().getValue().setCertificateTime("" + date.getTime());
                }
                setUpdateData(viewModel.getMutableLiveData().getValue());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).setType(new boolean[]{true, true, true, false, false, false})
                .setCancelText("取消").setSubCalSize(15)
                .setCancelColor(getResources().getColor(R.color.color_tab_two))
                .setTitleText(title)
                .setTitleColor(getResources().getColor(R.color.color_tab_three)).setTitleSize(15)
                .setSubmitText("确定")
                .setSubmitColor(getResources().getColor(R.color.color_main))
//                .setRangDate(dayCalendar, null)
                .build();
        timePicker.show();
    }

    //单层选择
    private void showOnlyTypeWin(List<PickSelectEntity> selectedDate, String type, String name) {
        OptionsPickerView businessOption = new OptionsPickerBuilder(this, (options1, options2, options3, v) -> {
            String content = selectedDate.get(options1).getProjectscale();
            String no = selectedDate.get(options1).getNo();
            switch (type) {
                case "类型":
                    listPickSelectEntity = selectedDate.get(options1);
                    if (listPickSelectEntity == null || listPickSelectEntity.getChildren() == null || listPickSelectEntity.getChildren().size() == 0){
                        viewModel.getMutableLiveData().getValue().setQualificationClassName("");
                        viewModel.getMutableLiveData().getValue().setQualificationClassNo("");
                        binding.tvFidleGroup.setVisibility(View.GONE);
                    }else{
                        binding.tvFidleGroup.setVisibility(View.VISIBLE);
                    }
                    if (!content.equals(viewModel.getMutableLiveData().getValue().getType())){
                        viewModel.getMutableLiveData().getValue().setQualificationClassName("");
                        viewModel.getMutableLiveData().getValue().setQualificationClassNo("");
                    }
                    viewModel.getMutableLiveData().getValue().setType(content);
                    viewModel.getMutableLiveData().getValue().setQualificationTypeNo(no);
                    break;
                case "等级":
                    viewModel.getMutableLiveData().getValue().setQualificationClassName(content);
                    viewModel.getMutableLiveData().getValue().setQualificationClassNo(no);
                    break;
                case "权限":
                    viewModel.getMutableLiveData().getValue().setShowPermissionName(content);
                    viewModel.getMutableLiveData().getValue().setShowPermission(no);
                    break;
                case "单位":
                    viewModel.getMutableLiveData().getValue().setRecordEnterName(content);
                    viewModel.getMutableLiveData().getValue().setRecordEnterNo(no);
                    break;
            }
            //viewModel.getMutableLiveData().postValue(viewModel.getMutableLiveData().getValue());
            setUpdateData(viewModel.getMutableLiveData().getValue());
        }).setCancelText("取消").setSubCalSize(15)
                .setCancelColor(getResources().getColor(R.color.color_tab_two))
                .setTitleText(name)
                .setTitleColor(getResources().getColor(R.color.color_tab_three)).setTitleSize(15)
                .setSubmitText("确定")
                .setSubmitColor(getResources().getColor(R.color.color_main))
                .build();
        businessOption.setPicker(selectedDate);
        businessOption.setSelectOptions(0);
        businessOption.show();
    }

    //单层选择
    private void showOnlyOneTypeWin(List<PickSelectEntity<List<PickSelectEntity>>> selectedDate, String type, String name) {
        OptionsPickerView businessOption = new OptionsPickerBuilder(this, (options1, options2, options3, v) -> {
            String content = selectedDate.get(options1).getProjectscale();
            String no = selectedDate.get(options1).getNo();
            switch (type) {
                case "行业类型":
                     binding.tvHangyeType.getContent_edt().setText(content);
                     viewModel.getMutableLiveData().getValue().setIndustryTypeNo(no);
                     viewModel.getMutableLiveData().getValue().setIndustryTypeName(content);
                    break;
                case "类型":
                    listPickSelectEntity = selectedDate.get(options1);
                    if (listPickSelectEntity == null || listPickSelectEntity.getChildren() == null || listPickSelectEntity.getChildren().size() == 0){
                        viewModel.getMutableLiveData().getValue().setQualificationClassName("");
                        viewModel.getMutableLiveData().getValue().setQualificationClassNo("");
                        binding.tvFidleGroup.setVisibility(View.GONE);
                    }else{
                        binding.tvFidleGroup.setVisibility(View.VISIBLE);
                    }
                    if (!content.equals(viewModel.getMutableLiveData().getValue().getType())){
                        viewModel.getMutableLiveData().getValue().setQualificationClassName("");
                        viewModel.getMutableLiveData().getValue().setQualificationClassNo("");
                    }
                    viewModel.getMutableLiveData().getValue().setType(content);
                    viewModel.getMutableLiveData().getValue().setQualificationTypeNo(no);
                    break;
                case "等级":
                    viewModel.getMutableLiveData().getValue().setQualificationClassName(content);
                    viewModel.getMutableLiveData().getValue().setQualificationClassNo(no);
                    break;
                case "权限":
                    viewModel.getMutableLiveData().getValue().setShowPermissionName(content);
                    viewModel.getMutableLiveData().getValue().setShowPermission(no);
                    break;
                case "单位":
                    onePos = options1;
                    twoPos = options2;
                    if (selectedDate.get(options1) != null && selectedDate.get(options1).getChildren() != null) {
                        if (selectedDate.get(options1).getChildren().size() > twoPos) {
                            viewModel.getMutableLiveData().getValue().setRecordEnterName(selectedDate.get(options1).getChildren().get(options2).getProjectscale());
                            viewModel.getMutableLiveData().getValue().setRecordEnterNo(selectedDate.get(options1).getChildren().get(options2).getNo());
                        }
                    }
                    viewModel.getMutableLiveData().getValue().setProvinceName(content);
                    viewModel.getMutableLiveData().getValue().setProvinceId(no);
                    break;
            }
            setUpdateData(viewModel.getMutableLiveData().getValue());
        }).setCancelText("取消").setSubCalSize(15)
                .setCancelColor(getResources().getColor(R.color.color_tab_two))
                .setTitleText(name)
                .setTitleColor(getResources().getColor(R.color.color_tab_three)).setTitleSize(15)
                .setSubmitText("确定")
                .setSubmitColor(getResources().getColor(R.color.color_main))
                .build();

        if ("单位".equals(type)) {
            businessOption.setPicker(oneData, twoData);
            businessOption.setSelectOptions(onePos, twoPos);
        } else {
            businessOption.setPicker(selectedDate);
            businessOption.setSelectOptions(0);
        }
        businessOption.show();
    }

    //展开popuwindw
    private void showAlbumPop() {
        //判断是否在未展示的情况
        if (!selectAlbumPopWindow.isShowing()) {
            //满足条件展示
            selectAlbumPopWindow.showDownPopwindow(binding.grayLayout, true);
        }
    }

    private void initAlbumPop() {
        if (selectAlbumPopWindow == null) {
            //满足条件，创建SelectAlbumPopWindow
            selectAlbumPopWindow = new SelectAlbumPopWindow(this);
        }
        //设置背景
        selectAlbumPopWindow.setBgView(binding.grayLayout);
        //设置点击回调
        selectAlbumPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                int type = (int) key1;
                if (type == 0) {
                    pictureSelector(true, 500, 1);
                    selectAlbumPopWindow.dismiss();
                }
            }
        });
    }

    private void showAvatar(String avatar) {
        viewModel.getMutableLiveData().getValue().setImage(avatar);
        viewModel.getMutableLiveData().postValue(viewModel.getMutableLiveData().getValue());
        if (TextUtils.isEmpty(avatar)) {
            binding.ivDelect.setVisibility(View.GONE);
            binding.ivUpdataImage.setImageResource(R.drawable.sck);
        } else {
            binding.ivDelect.setVisibility(View.VISIBLE);
            //Glide展示图片
            Glide.with(this)
                    //加载设置宽高限制
                    .load(avatar + "?x-oss-process=image/resize,m_fixed,h_100")
                    //显示圆图
//                    .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                    //设置为头像
                    .into(binding.ivUpdataImage);
        }
    }

    //结果回调
    @Override
    protected void onPictureSelectorResult(List<LocalMedia> selectList) {
        // 获取到图片的本地路径
        String path = selectList.get(0).getCompressPath();
        //保存在文件中
        if (!TextUtils.isEmpty(path)) {
            File file = new File(path);
            //接口上传文件
            viewModel.uploadAvatar(getActivity(), file);
            //上传时，出现上传弹窗
            DialogHelper.showProgressDialog(this, null, "头像上传中...", 0, false, null).setCanceledOnTouchOutside(false);
        } else {
            MToast.showToast(getApplicationContext(), "获取图片失败！");
        }
    }
}
